package com.hyfrogx.modules.zoom.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hyfrogx.common.exception.MyException;
import com.hyfrogx.common.utils.R;
import com.hyfrogx.modules.app.annotation.Login;
import com.hyfrogx.modules.sys.controller.AbstractController;
import com.hyfrogx.modules.zoom.entity.HyZoomMeeting;
import com.hyfrogx.modules.zoom.service.HyZoomMeetingService;
import com.hyfrogx.modules.zoom.service.HyZoomUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ：Li9527
 * @date ：Created in 2019-11-18 17:13
 * @description：zoom的api管理
 * @modified By：
 * @version: 1.0.0
 */
@RestController
@RequestMapping("/sys/zoom")
@Api(value = "zoom的api管理controller", tags = {"系统api级别--zoom的api管理[慎用]"})
public class ZoomApiController extends AbstractController {

    @Autowired
    HyZoomUserService zoomUserService;

    @Autowired
    HyZoomMeetingService meetingService;


    /**
     * 创建zoom用户
     *
     * @param uid
     * @param meeting_capacity
     * @throws Exception
     */
    @PostMapping("createUser")
    @ApiOperation("交互api接口,创建zoom账号,慎用，调一次就一个月几千块")
    public R createUser(@RequestParam(value = "uid", required = true) Long uid, @RequestParam(value = "meeting_capacity", required = true) Integer meeting_capacity) {
        try {
            String result = zoomUserService.creatZoomUser(uid, meeting_capacity);
            if (result != null) {
                return R.ok(result);
            } else {
                return R.error("创建账号失败");
            }
        } catch (MyException e) {
            return R.error(e.getMsg());
        }
    }

    /**
     * 删除用户
     *
     * @param hostid
     * @throws MyException
     */
    @PostMapping("deleteUser")
    @ApiOperation("交互api接口,删除zoom账号,非web端调用")
    public R deleteUser(@RequestParam(value = "hostid", required = true) String hostid) {
        try {
            Integer result = zoomUserService.deleteZoomUser(hostid);
            if (result > 0) {
                return R.ok("删除账号成功");
            } else {
                return R.error("创建账号失败");
            }
        } catch (MyException e) {
            return R.error(e.getMsg());
        }
    }

    /**
     * 删除会议室及账号
     *
     * @param hostid
     * @throws MyException
     */
    @PostMapping("deleteZoom")
    @ApiOperation("删除会议室及账号")
    public R deleteZoom(@RequestParam(value = "hostid", required = true) String hostid) {
        try {
            // 检查会议室是否到达截止时间
            HyZoomMeeting hyZoomMeeting = meetingService.getOne(new QueryWrapper<HyZoomMeeting>().eq("hostId", hostid));
            if (hyZoomMeeting.getEndat().getTime() < System.currentTimeMillis()) {
                // 已过期可以删除会议室及账号
                zoomUserService.deleteZoomUser(hyZoomMeeting.getHostid());
                // 然后删除会议室
                meetingService.removeById(hyZoomMeeting.getId());
                return R.ok("删除成功");
            } else {
                return R.error("该会议室尚未过期");
            }
        } catch (MyException e) {
            return R.error(e.getMsg());
        }
    }


    /**
     * 获取用户token
     *
     * @param hostid
     * @throws MyException
     */
    @PostMapping("getusertoken")
    @ApiOperation("交互api接口,获取用户token")
    public R getusertoken(@RequestParam(value = "hostid", required = true) String hostid) {
        try {
            String result = zoomUserService.getusertoken(hostid);
            if (result != null) {
                return R.ok(result);
            } else {
                return R.error("获取token失败");
            }
        } catch (MyException e) {
            return R.error(e.getMsg());
        }
    }

    /**
     * 获取用户zak
     *
     * @param hostid
     * @throws MyException
     */
    @PostMapping("getuserZak")
    @ApiOperation("交互api接口,获取用户zak")
    public R getuserZak(@RequestParam(value = "hostid", required = true) String hostid) {
        try {
            String result = zoomUserService.getuserZak(hostid);
            if (result != null) {
                return R.ok(result);
            } else {
                return R.error("获取zak失败");
            }
        } catch (MyException e) {
            return R.error(e.getMsg());
        }
    }

    /**
     * 获取用户方数
     *
     * @param hostid
     * @throws MyException
     */
    @PostMapping("getUserMeetingCapacity")
    @ApiOperation("交互api接口,获取用户方数")
    public R getUserMeetingCapacity(@RequestParam(value = "hostid", required = true) String hostid) {
        try {
            Integer result = zoomUserService.getUserMeetingCapacity(hostid);
            if (result > 0) {
                return R.ok(result.toString());
            } else {
                return R.error("获取zak失败");
            }
        } catch (MyException e) {
            return R.error(e.getMsg());
        }
    }

    /**
     * 获取用户列表
     *
     * @throws MyException
     */
    @GetMapping("getUserList")
    @ApiOperation("交互api接口,获取用户列表")
    public R getUserList() {
        try {
            String result = zoomUserService.getUserList();
            if (result != null) {
                return R.ok(result);
            } else {
                return R.error("获取zak失败");
            }
        } catch (MyException e) {
            return R.error(e.getMsg());
        }
    }

    /**
     * 升级Zoom用户
     *
     * @param hostid
     * @param meetingcapacity
     * @throws MyException
     */
    @PostMapping("updateUser")
    @ApiOperation("交互api接口,升级Zoom账号方数")
    public R updateUser(@RequestParam(value = "hostid", required = true) String hostid, Integer meetingcapacity) {

        try {
            Integer result = zoomUserService.updateUser(hostid, meetingcapacity);
            if (result > 0) {
                return R.ok(result.toString());
            } else {
                return R.error("获取zak失败");
            }
        } catch (MyException e) {
            return R.error(e.getMsg());
        }
    }


    /**
     * 根据会议室主键id修改会议室状态
     *
     * @param id
     * @throws MyException
     */
    @Login
    @PostMapping("updateCurrentInfo")
    @ApiOperation("交互api接口,<先去zoom查实时状态>根据会议室主键id重置会议室")
    public R updateCurrentInfo(@RequestParam(value = "id", required = true) Integer id) {
        try {
            // 查询出会议对象
            HyZoomMeeting meeting = meetingService.getById(id);
            if (meeting == null) {
                return R.error("会议室id错误");
            }
            // 查询该场会议zoom那边的实时状态
            int currentStatus = meetingService.getTrueStatusFromZoom(meeting);
            if (currentStatus == 0) {
                // 使会议室处于空闲中
                meeting.setMeetstatus(0);
                // 修改会议室临时拥有者为空
                meeting.setCurrentuid(null);
                // 修改会议主题为空
                meeting.setLocalname("");
                // 修改会议室状态为空闲
                meeting.setMeetstatus(0);
                // 不管是否设密码,都重置为没设密码
                meeting.setIshavepsd(0);
                // 不管是否拥有密码,密码置空
                meeting.setPassword(null);
                // 不管是否收费,重新开会都置0
                meeting.setIscharge(0);
                // 收费金额取消,重新开会都置0
                meeting.setPrice(0);
                // 外层时间戳取消
                meeting.setHymeetid(null);
                Boolean result = meetingService.updateById(meeting);
                return R.ok("重置成功");
            } else {
                return R.error("当前有会议开启,禁止重置");
            }
        } catch (MyException e) {
            return R.error(e.getMsg());
        }
    }

    /**
     * Zoom结束一场会议
     *
     * @param id
     * @throws MyException
     */
    @Login
    @PostMapping("/endCurrentMeet/{id}")
    @ApiOperation("交互api接口,结束当前会议")
    public R endCurrentMeet(@PathVariable Integer id) {
        try {
            // 查询出会议对象
            HyZoomMeeting meeting = meetingService.getById(id);
            if (meeting == null) {
                return R.error("会议室id错误");
            }
            int result = meetingService.closeMeetZoom(meeting);
            if (result > 0) {
                return R.ok("结束成功");
            } else {
                return R.error("结束失败");
            }
        } catch (MyException e) {
            return R.error(e.getMsg());
        }
    }

    /**
     * Zoom删除一场会议
     *
     * @param id
     * @throws MyException
     */
    @Login
    @PostMapping("/deleteCurrentMeet/{id}")
    @ApiOperation("删除一场会议(开会中不能删除,得先结束会议")
    public R deleteCurrentMeet(@PathVariable Integer id) {
        try {
            // 查询出会议对象
            HyZoomMeeting meeting = meetingService.getById(id);
            if (meeting == null) {
                return R.error("会议室id错误");
            }
            int result = meetingService.deleteMeetZoom(meeting);
            if (result > 0) {
                return R.ok("删除成功");
            } else {
                return R.error("删除失败");
            }
        } catch (MyException e) {
            return R.error(e.getMsg());
        }
    }

}
